/* Preliminary transformations of dyadic data */
clear all
set matsize 2000
use REFUGEES_COMPLETE.dta
*drop MERGE*

encode Origin, generate(origin)
encode Country, generate(country)

egen Varpanel=concat(Origin Country)
encode Varpanel, generate(varpanel)
xtset varpanel Year

* Generate the Pre-Sample Means (1951-1989) of refugee flows by origin and destination 
sort Origin
by Origin: egen pmo_aux=mean(Refugees) if Year<1990

sort Origin
by Origin: egen pmo=mean(pmo_aux)

sort Country
by Country: egen pmd_aux=mean(Refugees) if Year<1990

sort Country
by Country: egen pmd=mean(pmd_aux)

* Generate the Pre-Sample Means (1951-2001) of refugee flows by origin and destination 
sort Origin
by Origin: egen pmo_aux2=mean(Refugees) if Year<2002

sort Origin
by Origin: egen pmo5101=mean(pmo_aux2)

sort Country
by Country: egen pmd_aux2=mean(Refugees) if Year<2002

sort Country
by Country: egen pmd5101=mean(pmd_aux2)

* Generate the Pre-Sample Means (1992-2001) of refugee flows by origin and destination 
sort Origin
by Origin: egen pmo_aux3=mean(Refugees) if Year>1991 & Year<2002

sort Origin
by Origin: egen pmo9201=mean(pmo_aux3)

sort Country
by Country: egen pmd_aux3=mean(Refugees) if Year>1991 & Year<2002

sort Country
by Country: egen pmd9201=mean(pmd_aux3)


/* Lograrithmic transformations */

gen logref = log(Refugees)
gen logPOPA = log(Population)
gen logPOPB = log(Population_1)
gen L_GDP_PC_PPP = log(GDP_PC_PPP)
gen L_GDP_PC_PPP_01 = log(GDP_PC_PPP_01)
gen logpmo=log(pmo)
gen logpmd=log(pmd)
gen logpmo5101=log(pmo5101)
gen logpmd5101=log(pmd5101)
gen logpmo9201=log(pmo9201)
gen logpmd9201=log(pmd9201)

gen L_area=log(areakm2)
gen L_area_01=log(areakm2_01)

gen L_Ethnic=log(Ethnic)
gen L_Language=log(Language)
gen L_Religion=log(Religion)
gen L_Landlock=log(lcr100km)

gen L_Ethnic_01=log(Ethnic_01)
gen L_Language_01=log(Language_01)
gen L_Religion_01=log(Religion_01)
gen L_Landlock_01=log(lcr100km_01)

gen distmkm = dist/1000 /* distance in thousand kilometers */
gen L_dist =log(distmkm)

/* Generating sets of dummies */
replace polity2=polity2+11
replace polity2_01=polity2_01+11
quiet tab polity2, generate(pol)
quiet tab polity2_01, generate(pol_)
quiet tab CL, generate(CL)
quiet tab PR, generate(PR)
quiet tab CL_01, generate(CL_01)
quiet tab PR_01, generate(PR_01)
* actotal has no values equal to 11 or 12
* actotal has very few observation with values equal to 10, 13 or 14
replace actotal=7 if actotal==8 | actotal==9
replace actotal=8 if actotal==10 | actotal==13 | actotal==14
quiet tab actotal, generate(act)
replace actotal_01=7 if actotal_01==8 | actotal_01==9
replace actotal_01=8 if actotal_01==10 | actotal_01==13 | actotal_01==14
quiet tab actotal_01, generate(act_01)

* generating Conflictlevel
gen Conflictlevel=Conflict_ORIGIN*IntensityLevel if Conflict_ORIGIN==1
replace Conflictlevel=Conflict_ORIGIN if Conflict_ORIGIN==0
quiet tab Conflictlevel, generate(Conflictlevel)
* generating Conflictlevel_01
gen Conflictlevel_01=Conflict_DESTINATION*IntensityLevel_01 if Conflict_DESTINATION==1
replace Conflictlevel_01=Conflict_DESTINATION if Conflict_DESTINATION==0
quiet tab Conflictlevel_01, generate(Conflictlevel_01)


***




